public class Test3_1 {
    public static int duplicate(int[] number){
        if (number == null || number.length  < 1) return -1;
        for (int i : number){
            if ( i < 0 || i > number.length-1) return  -1;//判断输入的数字是否在范围内
        }
        for (int i = 0; i< number.length ;i++){
            while(number[i] != i){
                //判断是否想等，如果相等则返回
                if (number[i] == number[number[i]]) return number[i];
                //不想等则一直交换
                else swap(number,i,number[i]);
            }
        }
        return -1;
    }
    public static void swap(int[] data,int x,int y){
        int temp = data[x];
        data[x] = data[y];
        data[y] = temp;
    }

    public static void main(String[] args) {
        //int[] numbers1 = {2, 1, 3, 1, 4};
        //System.out.println(duplicate(numbers1));

        int[] numbers2 = {2, 4, 3, 1, 4};
        System.out.println(duplicate(numbers2));

    }
}
